<?php

if (!defined("MCTDESIGN")) die("# ");

if (isset($_POST['delete'])) {
	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;
 	for($i=1;$i<=$total;$i++){
		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';
		if (!empty($checkbox[$i])) {
			$mysql->query("DELETE FROM ".$tb_fix."_product WHERE pro_id='".$checkbox[$i]."'");
		}
	}
	$html_body = _html_trans("Đã xoá các sản phẩm thành công !");
	return false;
}
if (isset($_POST['update'])) {
	$pro_key = isset($_POST["pro_key"]) ? fixquotes(stripslashes($_POST["pro_key"])) : "";
	$pro_name = isset($_POST["pro_name"]) ? fixquotes(stripslashes($_POST["pro_name"])) : "";
	$pro_catid = isset($_POST["pro_catid"]) ? stripslashes($_POST["pro_catid"]) : '';
	$pro_manufacturerid = isset($_POST["pro_manufacturerid"]) ? intval($_POST["pro_manufacturerid"]) : 0;
	$pro_content_desc = isset($_POST["pro_content_desc"]) ? fixquotes(m_htmlchars(stripslashes($_POST["pro_content_desc"]))) : "";
	$pro_content = isset($_POST["pro_content"]) ? fixquotes(m_htmlchars(stripslashes($_POST["pro_content"]))) : "";
	$pro_authors = isset($_POST["pro_authors"]) ? fixquotes(stripslashes($_POST["pro_authors"])) : "";
	$pro_price = isset($_POST["pro_price"]) ? stripslashes($_POST["pro_price"]) : 0;
	$pro_pagecount = isset($_POST["pro_pagecount"]) ? stripslashes($_POST["pro_pagecount"]) : 0;
	$pro_promotion = isset($_POST["pro_promotion"]) ? stripslashes($_POST["pro_promotion"]) : "";
	$pro_promotion_vnd = isset($_POST["pro_promotion_vnd"]) ? intval($_POST["pro_promotion_vnd"]) : 0;
	$pro_promotion_usd = isset($_POST["pro_promotion_usd"]) ? intval($_POST["pro_promotion_usd"]) : 0;
	$pro_promotion_des = isset($_POST["pro_promotion_des"]) ? m_htmlchars($_POST["pro_promotion_des"]) : "";
	$pro_promotion_content = isset($_POST["pro_promotion_content"]) ? m_htmlchars($_POST["pro_promotion_content"]) : "";
	$pro_firm = isset($_POST["pro_firm"]) ? stripslashes($_POST["pro_firm"]) : "";
	$pro_store = isset($_POST["pro_store"]) ? stripslashes($_POST["pro_store"]) : "";
	$pro_warranty = isset($_POST["pro_warranty"]) ? intval(stripslashes($_POST["pro_warranty"])) : "";
	$pro_img = isset($_FILES['pro_img']['name'] ) ? $_FILES['pro_img']['name'] : '';
	$pro_tmp_img = isset($_FILES['pro_img']['tmp_name'] ) ? $_FILES['pro_img']['tmp_name'] : '';
	if (empty($pro_name) || empty($pro_catid) || empty($pro_content) || empty($id)) {
		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");
		return false;
	}
	if (!empty($pro_img)) {
		$pro_img = uploadIMG($pro_img,$pro_tmp_img,"product");
		$SQL_IMG = ",pro_images='$pro_img'";
	}
	list($catpid, $catrid, $catid)=explode("||",$pro_catid);
    $mysql->query("UPDATE ".$tb_fix."_product SET pro_key='$pro_key',pro_catid='$catid',pro_catpid='$catpid',pro_catrid='$catrid',pro_manufacturerid='$pro_manufacturerid',pro_name='$pro_name',pro_content_desc='$pro_content_desc',pro_content='$pro_content',pro_authors='$pro_authors',pro_pagecount='$pro_pagecount',pro_price='$pro_price',pro_promotion='$pro_promotion_vnd',pro_promotion_vnd='$pro_promotion_vnd',pro_promotion_content='$pro_promotion_content',pro_promotion_des='$pro_promotion_des',pro_promotion_usd='$pro_promotion_usd',pro_firm='$pro_firm',pro_store='$pro_store',pro_warranty='$pro_warranty' $SQL_IMG WHERE pro_id='".$id."'");
	$html_body = _html_trans("Đã cập nhật thông tin sản phẩm thành công !","admin.php?obj=Product");
	return false;
}
if (isset($_POST['add'])) {
	$pro_key = isset($_POST["pro_key"]) ? fixquotes(stripslashes($_POST["pro_key"])) : "";
	$pro_name = isset($_POST["pro_name"]) ? fixquotes(stripslashes($_POST["pro_name"])) : "";
	$pro_catid = isset($_POST["pro_catid"]) ? stripslashes($_POST["pro_catid"]) : '';
	$pro_manufacturerid = isset($_POST["pro_manufacturerid"]) ? intval($_POST["pro_manufacturerid"]) : 0;
	$pro_content_desc = isset($_POST["pro_content_desc"]) ? fixquotes(m_htmlchars(stripslashes($_POST["pro_content_desc"]))) : "";
	$pro_content = isset($_POST["pro_content"]) ? fixquotes(m_htmlchars(stripslashes($_POST["pro_content"]))) : "";
	$pro_authors = isset($_POST["pro_authors"]) ? fixquotes(stripslashes($_POST["pro_authors"])) : "";
	$pro_price = isset($_POST["pro_price"]) ? stripslashes($_POST["pro_price"]) : 0;
	$pro_pagecount = isset($_POST["pro_pagecount"]) ? stripslashes($_POST["pro_pagecount"]) : 0;
	$pro_promotion = isset($_POST["pro_promotion"]) ? stripslashes($_POST["pro_promotion"]) : "";
	$pro_promotion_vnd = isset($_POST["pro_promotion_vnd"]) ? intval($_POST["pro_promotion_vnd"]) : 0;
	$pro_promotion_usd = isset($_POST["pro_promotion_usd"]) ? intval($_POST["pro_promotion_usd"]) : 0;
	$pro_promotion_des = isset($_POST["pro_promotion_des"]) ? m_htmlchars($_POST["pro_promotion_des"]) : "";
	$pro_promotion_content = isset($_POST["pro_promotion_content"]) ? m_htmlchars($_POST["pro_promotion_content"]) : "";
	$pro_firm = isset($_POST["pro_firm"]) ? stripslashes($_POST["pro_firm"]) : "";
	$pro_store = isset($_POST["pro_store"]) ? stripslashes($_POST["pro_store"]) : "";
	$pro_warranty = isset($_POST["pro_warranty"]) ? intval(stripslashes($_POST["pro_warranty"])) : "";
	$pro_img = isset($_FILES['pro_img']['name'] ) ? $_FILES['pro_img']['name'] : '';
	$pro_tmp_img = isset($_FILES['pro_img']['tmp_name'] ) ? $_FILES['pro_img']['tmp_name'] : '';
	if (empty($pro_name) || empty($pro_catid) || empty($pro_img) || empty($pro_content)) {
		$html_body = _html_trans("Dữ liệu bạn nhập chưa đầy đủ !");
		return false;
	}
	$pro_img = uploadIMG($pro_img,$pro_tmp_img,"product");

	list($catpid, $catrid, $catid)=explode("||",$pro_catid);
	$timenow = time();
	$mysql->query("INSERT INTO ".$tb_fix."_product (pro_id , pro_key , pro_catid, pro_catpid, pro_catrid, pro_manufacturerid, pro_admid, pro_name, pro_date,pro_content_desc,pro_content, pro_authors, pro_pagecount, pro_images, pro_price, pro_promotion, pro_promotion_usd, pro_promotion_vnd, pro_promotion_content, pro_promotion_des, pro_firm,pro_store, pro_warranty) VALUES ('','".$pro_key."','".$catid."','".$catpid."','".$catrid."','".$pro_manufacturerid."','".$admin_id."','".$pro_name."','".$timenow."','".$pro_content_desc."','".$pro_content."','".$pro_authors."','".$pro_pagecount."','".$pro_img."','".$pro_price."','".$pro_promotion_vnd."','".$pro_promotion_usd."','".$pro_promotion_vnd."','".$pro_promotion_content."','".$pro_promotion_des."','".$pro_firm."','".$pro_store."','".$pro_warranty."')");
	$html_body = _html_trans("Đã thêm thông tin sản phẩm thành công !","admin.php?obj=Product");
	return false;
}

function _list_select_Cat($_catid="",$parent="0") {
	global $mysql, $tb_fix;
	$q = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='$parent' ORDER BY cat_order ASC");
	$t = $mysql->num_rows($q);
	$_list = "";
	if ($t>0) {
		while ($r = $mysql->fetch_array($q)) {
			if ($_catid == $r["cat_id"]) $_list .= "<option value='".$r["cat_id"]."||".$r["cat_id"]."||".$r["cat_id"]."' selected>|-- ".$r["cat_title"]."</option>";
			else $_list .= "<option value='".$r["cat_id"]."||".$r["cat_id"]."||".$r["cat_id"]."'>|-- ".$r["cat_title"]."</option>";
			$q_s = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='".$r["cat_id"]."' ORDER BY cat_order ASC");
			while ($r_s = $mysql->fetch_array($q_s)) {
				if ($_catid == $r_s["cat_id"]) $_list .= "<option value='".$r["cat_id"]."||".$r_s["cat_id"]."||".$r_s["cat_id"]."' selected>&nbsp;&nbsp;&nbsp;&nbsp;|-- ".$r_s["cat_title"]."</option>";
				else  $_list .= "<option value='".$r["cat_id"]."||".$r_s["cat_id"]."||".$r_s["cat_id"]."'>&nbsp;&nbsp;&nbsp;&nbsp;|-- ".$r_s["cat_title"]."</option>";
				$q_ss = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='".$r_s["cat_id"]."' ORDER BY cat_order ASC");
				while ($r_ss = $mysql->fetch_array($q_ss)) {
					if ($_catid == $r_ss["cat_id"]) $_list .= "<option value='".$r["cat_id"]."||".$r_s["cat_id"]."||".$r_ss["cat_id"]."' selected>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|-- ".$r_ss["cat_title"]."</option>";
					else  $_list .= "<option value='".$r["cat_id"]."||".$r_s["cat_id"]."||".$r_ss["cat_id"]."'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|-- ".$r_ss["cat_title"]."</option>";
				}
			}
		}
	}
	return $_list;
}
function _list_select_Manufacturer($_manuid="") {
	global $mysql, $tb_fix;
	$q = $mysql->query("SELECT * FROM ".$tb_fix."_manufacturer ORDER BY manu_order ASC");
	$t = $mysql->num_rows($q);
	$_list = "";
	if ($t>0) {
		while ($r = $mysql->fetch_array($q)) {
			if ($_manuid == $r["manu_id"]) $_list .= "<option value='".$r["manu_id"]."' selected>|-- ".$r["manu_title"]."</option>";
			else $_list .= "<option value='".$r["manu_id"]."'>|-- ".$r["manu_title"]."</option>";
		}
	}
	return $_list;
}

function _html_manageProduct()
{
	global $dir_tpl, $mysql, $tb_fix, $page, $shop_usd,$temp;
	$kw = isset($_GET["kw"]) ? fixquotes(m_htmlchars($_GET["kw"])) : "";
	$_limit = 20;
	$temp_body = $temp;
	$temp_body->assign( 'base_tpl', "".$dir_tpl."" );
	if (empty($kw))	{
	   if(isset($_POST['sapxep_sp'])){ // sap xep
	       $sapxep = (int)$_POST['sapxep_sp'];
           $where = "";
           if($sapxep == 1){ // sap xep theo sp moi
                $where = " ";
           }
           if($sapxep == 2){ // sap xep theo sp noi bat
                $where = " AND p.pro_focus = 1";
           }
           if($sapxep == 3){ // sap xep theo sp ban chay
                $where = " AND p.pro_sale = 1";
           }
           if($sapxep == 4){ // sap xep theo sp khuyen mai
                $where = " AND p.pro_offer = 1";
           }
                      
	   }else{
	       $where = "";
	   }       
       
	   $q = $mysql->query("SELECT * FROM ".$tb_fix."_product AS p
                            JOIN ".$tb_fix."_category AS c
                                ON p.pro_catid=c.cat_id
                            LEFT JOIN ".$tb_fix."_admin AS a 
                                ON p.pro_admid=a.a_id
                            WHERE p.pro_name!='' 
                            $where 
                            ORDER BY pro_id DESC");  
	}
	else{ // tim kiem
	   $q = $mysql->query("SELECT * FROM ".$tb_fix."_product AS p
                            JOIN ".$tb_fix."_category AS c 
                                ON  p.pro_catid=c.cat_id
                            LEFT JOIN ".$tb_fix."_admin AS a 
                                ON p.pro_admid=a.a_id
                            WHERE p.pro_name RLIKE '^[0-9]' OR p.pro_name LIKE '%".$kw."%' OR p.pro_name LIKE '".$kw."%'
                            ORDER BY pro_id DESC");
	}
	$t = $mysql->num_rows($q);
	if ($t>0) {
		$_start = ($page-1) * $_limit ;
		$_end   = $page*$_limit;
		$num_pages = ceil($t/$_limit) ;
		if ($page > $num_pages) $page=$num_pages;
		if ($page < 1 ) $page=1;
		$page_view = Paginate_Admin($t, $_limit, $page, "page");
		$html = "";
		$i = 0;
		$_c = 0;
		while ($r = $mysql->fetch_array($q)) {
			if ($i>=$_start) {
				$_c++;
				if ($r["pro_focus"] == 1) {
					$focus = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},0,\"pro_focus\")'><img src=\"_images/activate.png\" title=\"Loại khỏi danh sách sản phầm Nổi bật\" align=\"center\" border=\"0\"></a>";
				}
				else $focus = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},1,\"pro_focus\")'><img src=\"_images/inactive.png\" title=\"Đưa vào danh sách sản phầm Nổi bật\" align=\"center\" border=\"0\"></a>";
				if ($r["pro_new"] == 1) {
					$new = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},0,\"pro_new\")'><img src=\"_images/activate.png\" title=\"Loại khỏi danh sách sản phẩm mới xuất hiện\" align=\"center\" border=\"0\"></a>";
				}
				else $new = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},1,\"pro_new\")'><img src=\"_images/inactive.png\" title=\"Đưa vào danh sách sản phẩm mới xuất hiện\" align=\"center\" border=\"0\"></a>";
				if ($r["pro_sale"] == 1) {
					$sale = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},0,\"pro_sale\")'><img src=\"_images/activate.png\" title=\"Loại khỏi danh sách sản phẩm bán chạy nhất\" align=\"center\" border=\"0\"></a>";
				}
				else $sale = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},1,\"pro_sale\")'><img src=\"_images/inactive.png\" title=\"Đưa vào danh sách sản phẩm bán chạy nhất\" align=\"center\" border=\"0\"></a>";
				if ($r["pro_offer"] == 1) {
					$offer = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},0,\"pro_offer\")'><img src=\"_images/activate.png\" title=\"Loại khỏi danh sách sản phẩm khuyễn mãi\" align=\"center\" border=\"0\"></a>";
				}
				else $offer = "<a href=\"javascript:;\" onclick='xajax_change_product({$r["pro_id"]},1,\"pro_offer\")'><img src=\"_images/inactive.png\" title=\"Đưa vào danh sách sản phẩm khuyễn mãi\" align=\"center\" border=\"0\"></a>";
				if ($r["pro_promotion_vnd"]!=0) {
					$price = $r["pro_promotion_vnd"];
				}
				else {
					$price = $r["pro_price"]*$shop_usd;
					$html_price = "<br><font style=\"color:#ff6104; font-size: 12px; font-weight:bold;\">".$price." VNĐ</font>";
					if ($r["pro_promotion_usd"]!=0) {
						$price = $r["pro_promotion_usd"]*$shop_usd;
					}
				}
				$price = number_format($price, 0, ',', ',');
				$html .= "<tr class=\"row1\">";
				$html .= "<td>$_c</td>";
				$html .= "<td width=\"30%\" align=\"left\" colspan=\"2\"><div style=\"margin-left:5px;\"><a href=\"admin.php?obj=Product&act=edit&id=".$r["pro_id"]."\">".$r["pro_name"]."</div></td>";
				//$html .= "<td><font color=\"red\">".$r["username"]."</font></td>";
				$html .= "<td style=\"text-align: center;\"><font color=\"red\">".$price."</font> <b>VNĐ</b></td>";
				$html .= "<td style=\"text-align: center;\"><b>".$r["cat_title"]."</b></td>";
				$html .= "<td style=\"text-align: center;\"><span id='pro_focus_{$r["pro_id"]}'>$focus</span></td>";
				$html .= "<td style=\"text-align: center;\"><span id='pro_sale_{$r["pro_id"]}'>$sale</span></td>";
				//$html .= "<td><span id='pro_new_{$r["pro_id"]}'>$new</span></td>";
				$html .= "<td style=\"text-align: center;\"><span id='pro_offer_{$r["pro_id"]}'>$offer</span></td>";
				$html .= "<td style=\"text-align: center;\"><input type='checkbox' id='checkbox' name='checkbox_".$_c."' value='".$r["pro_id"]."'></td>";
				$html .= "</tr>";
			}
			$i++;
			if ($i>=$_end) break;
		}
		$html .= "<input type='hidden' name='total' value='".$_c."'>";
		$html .= "<tr class=\"row1\">";
		$html .= "<td align=\"right\" colspan=\"8\">
				<input name=\"CheckAll\" class=\"ui-state-default ui-corner-all\" type=\"button\" id=\"CheckAll\" value=\"Đánh Dấu Tất Cả\" onClick=\"checkall()\">
				<input name=\"UnCheckAll\" class=\"ui-state-default ui-corner-all\"  type=\"button\" id=\"UnCheckAll\" value=\"Bỏ Đánh Dấu\" onClick=\"uncheckall()\">
		</td>";
		$html .= "<td><input name='delete' class=\"ui-state-default ui-corner-all\"  type='submit' value=' Xoá ' onClick=\"return check_checkbox();\"></td>";
		$html .= "</tr>";
		//$html .= "<tr height=\"25\" class=\"row1\"><td colspan=\"8\"><ul class='paginator'>$page_view</ul></td></tr>";
		$temp_body->assign( 'list_product', "".$html."" );
        $temp_body->assign('page_view',$page_view);
	}
	else $temp_body->assign( 'list_product', "<tr><td colspan=\"8\" align=\"center\" class=\"no_data\"><p style='color: #ffffff;text-align: center;'>Chưa có thông tin sản phẩm nào !<br style='text-align: center' /><input type='button' class='cancel' value='trở lại' style='float: none' onclick=redirect('admin.php?obj=Product') /><p></td></tr>" );
    $temp_body->assign("sapxep",$sapxep);
    $temp_body->assign("breakcum",breakcum("Quản lý sản phẩm  "));
	$_html = $temp_body->fetch( '_adm_product.mct' );
	return $_html;
}

function _html_addProduct()
{
	global $dir_tpl, $mysql, $tb_fix, $temp;
	$temp_body = $temp;
	$temp_body->assign( 'base_tpl', "".$dir_tpl."" );
	$list_manufacturer = _list_select_Manufacturer();
	$temp_body->assign( 'list_manufacturer', "".$list_manufacturer."" );
	$list_cat = _list_select_Cat();
	$temp_body->assign( 'list_cat', "".$list_cat."" );
    $temp_body->assign("breakcum",breakcum("Thêm sản phẩm "));
	$_html = $temp_body->fetch( '_adm_product_add.mct' );
	return $_html;
}
function _html_editProduct($_id="")
{
	global $dir_tpl, $mysql, $tb_fix, $temp;
    $sql = "SELECT * FROM ".$tb_fix."_product AS p 
                LEFT JOIN ".$tb_fix."_category AS c
                    ON p.pro_catid=c.cat_id
            WHERE p.pro_id='$_id' 
            LIMIT 1";
	$q = $mysql->query($sql);
	$t = $mysql->num_rows($q);
	$_html = "";
	if ($t>0) {
		$r = $mysql->fetch_array($q);
		$temp_body = $temp;
		$temp_body->assign( 'base_tpl', "".$dir_tpl."" );
		$list_cat = _list_select_Cat($r["pro_catid"]);
		$list_manufacturer = _list_select_Manufacturer($r["pro_manufacturerid"]);
		$temp_body->assign( 'list_manufacturer', "".$list_manufacturer."" );
		$temp_body->assign(array(
								"pro_userid" => "".$r["pro_userid"]."",
								"pro_key" => "".htmlchars($r["pro_key"])."",
								"pro_name" => "".htmlchars($r["pro_name"])."",
								"pro_content_desc" => "".htmlchars($r["pro_content_desc"])."",
								"pro_content" => "".htmlchars($r["pro_content"])."",
								"pro_notes" => "".$r["notes"]."",
								"pro_authors" => "".$r["pro_authors"]."",
								"pro_pagecount" => "".$r["pro_pagecount"]."",
								"pro_price" => "".$r["pro_price"]."",
								"pro_promotion" => "".$r["pro_promotion"]."",
								"pro_promotion_usd" => "".$r["pro_promotion_usd"]."",
								"pro_promotion_vnd" => "".$r["pro_promotion_vnd"]."",
								"pro_promotion_des" => "".$r["pro_promotion_des"]."",
								"pro_promotion_content" => "".$r["pro_promotion_content"]."",
								"pro_firm" => "".$r["pro_firm"]."",
								"pro_store" => "".$r["pro_store"]."",
								"pro_warranty" => "".$r["pro_warranty"]."",
								"pro_id" => "".$r["pro_id"].""
							)
						);
		$pro_img = "";
		if ($r["pro_images"]!='') $pro_img = "<img src=\"".$r["pro_images"]."\" border=\"1px\" width=\"150px\" height=\"100px\">";
		$temp_body->assign( 'pro_img', "".$pro_img."" );
		$temp_body->assign( 'list_cat', "".$list_cat."" );
        $temp_body->assign("breakcum",breakcum("Sửa sản phẩm "));
		$_html = $temp_body->fetch( '_adm_product_edit.mct' );
	}
	else {
		$_html .= _html_trans("Không tồn tại thông tin sản phẩm này trong hệ thống !","admin.php?obj=Product");
	}
	return $_html;
}

if ($act=="add") {
	$html_body = _html_addProduct();
	return false;
}
if ($act=="edit") {
	$html_body = _html_editProduct($id);
	return false;
}
$html_body = _html_manageProduct();

?>